/**
 * 
 */
package DAO;

import java.sql.ResultSet;
import java.util.ArrayList;

import POJO.ChiTietPhieuDatHang;
import Util.DataProvider;

/**
 * @author THIEUQUANG
 * 
 */
public class ChiTietPhieuDatHangDAO
{
    public static ArrayList<ChiTietPhieuDatHang> SelectChiTietPhieuDatHangALL()
    {
        ArrayList<ChiTietPhieuDatHang> listChiTietPhieuDatHang = new ArrayList<ChiTietPhieuDatHang>();

        String sql = "SELECT * FROM CHITIETPHIEUDAT";

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();

            ResultSet rs = dp.ExecuteQuery(sql);
            while (rs.next())
            {
                ChiTietPhieuDatHang ctpdh = new ChiTietPhieuDatHang();

                ctpdh.setMaChiTietPD(rs.getInt("maChiTietPD"));
                ctpdh.setMaSanPham(rs.getInt("maSanPham"));
                ctpdh.setSoLuong(rs.getInt("soLuong"));
                ctpdh.setDonGia(rs.getFloat("donGia"));
                ctpdh.setMaDatHang(rs.getInt("maDatHang"));

                listChiTietPhieuDatHang.add(ctpdh);
            }

            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
        return listChiTietPhieuDatHang;
    }

    public static ChiTietPhieuDatHang SelectChiTietPhieuDatHangById(
            ChiTietPhieuDatHang chiTietPhieuDatHangID)
    {
        ChiTietPhieuDatHang ctpdh = new ChiTietPhieuDatHang();

        try
        {
            String sql = "SELECT * FROM CHITIETPHIEUDAT WHERE MACHITIETPD="
                    + chiTietPhieuDatHangID;
            DataProvider dp = new DataProvider();
            dp.open();

            ResultSet rs = dp.ExecuteQuery(sql);
            while (rs.next())
            {

                ctpdh.setMaChiTietPD(rs.getInt("maChiTietPD"));
                ctpdh.setMaSanPham(rs.getInt("maSanPham"));
                ctpdh.setSoLuong(rs.getInt("soLuong"));
                ctpdh.setDonGia(rs.getFloat("donGia"));
                ctpdh.setMaDatHang(rs.getInt("maDatHang"));
            }

            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return ctpdh;
    }

    public static boolean InsertChiTietPhieuDatHang(ChiTietPhieuDatHang ctpdh)
    {
        boolean kq = false;

        String sql = String
                .format("INSERT INTO CHITIETPHIEUDAT(MACHITIETPD, MASANPHAM, SOLUONG, DONGIA, MADATHANG) VALUES(%d, %d, %d, %f, %d)",
                        ctpdh.getMaChiTietPD(), ctpdh.getMaSanPham(),
                        ctpdh.getSoLuong(), ctpdh.getDonGia() * 1.0,
                        ctpdh.getMaDatHang());

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();
            int i = dp.executeUpdate(sql);
            if (i == 1)
                kq = true;
            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return kq;
    }

    public static boolean UpdateChiTietPhieuDathang(ChiTietPhieuDatHang ctpdh)
    {
        boolean kq = false;

        String sql = String
                .format("UPDATE CHITIETPHIEUDAT SET MASANPHAM=%d, SOLUONG=%d, DONGIA=%f, MADATHANG=%d WHERE MACHITIETPD='%s'",
                        ctpdh.getMaSanPham(), ctpdh.getSoLuong(),
                        ctpdh.getDonGia() * 1.0, ctpdh.getMaDatHang(),
                        ctpdh.getMaChiTietPD());

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();
            int i = dp.executeUpdate(sql);
            if (i == 1)
                kq = true;
            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return kq;
    }

    public static boolean DeleteChiTietPhieuDathang(String ChiTietPhieuDatHangID)
    {
        boolean kq = false;

        String sql = "DELETE FORM CHITIETPHIEUDAT WHERE MACHITIETPD="
                + ChiTietPhieuDatHangID;

        try
        {
            DataProvider dp = new DataProvider();
            dp.open();
            int i = dp.executeUpdate(sql);
            if (i == 1)
                kq = true;
            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }

        return kq;
    }

    public static ArrayList<ChiTietPhieuDatHang> SelectChiTietPhieuDatHangByMaDonHang(
            String maPhieuDatHang)
    {
        // TODO Auto-generated method stub
        ArrayList<ChiTietPhieuDatHang> list = new ArrayList<ChiTietPhieuDatHang>();
        String sql = "SELECT * FROM CHITIETPHIEUDAT WHERE MADATHANG="+ maPhieuDatHang;
        
        try
        {
            DataProvider dp = new DataProvider();
            dp.open();
            
            ResultSet rs = dp.ExecuteQuery(sql);
            while (rs.next())
            {
                ChiTietPhieuDatHang ctpdh = new ChiTietPhieuDatHang();
                
                ctpdh.setMaChiTietPD(rs.getInt("maChiTietPD"));
                ctpdh.setMaSanPham(rs.getInt("maSanPham"));
                ctpdh.setSoLuong(rs.getInt("soLuong"));
                ctpdh.setDonGia(rs.getFloat("donGia"));
                ctpdh.setMaDatHang(rs.getInt("maDatHang"));
                
                list.add(ctpdh);
            }
            
            dp.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }
        
        return list;
    }
}
